﻿using EnetaMvc.ApplicationServices;
using EnetaMvc.ApplicationServices.Dto;
using EnetaMvc.Web.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EnetaMvc.Web.Controllers
{
    public class HtmlControlsController : Controller
    {
        private readonly IContentService _contentService;

        public HtmlControlsController(IContentService contentService)
        {
            _contentService = contentService;
        }

        [EnetaAuthorize(Roles = "Administrator")]
        public ActionResult Index()
        {
            return View(_contentService.ListHtmlControls());
        }

        [EnetaAuthorize(Roles = "Administrator")]
        public ActionResult Create()
        {
            HtmlControlDto htmlControl = new HtmlControlDto();
            return View(htmlControl);
        }

        [EnetaAuthorize(Roles = "Administrator")]
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(HtmlControlDto model)
        {
            _contentService.SaveHtmlControl(model);
            return RedirectToAction("Index");
        }

        [EnetaAuthorize(Roles = "Administrator")]
        public ActionResult Edit(long id)
        {
            return View(_contentService.GetHtmlControl(id));
        }

        [EnetaAuthorize(Roles = "Administrator")]
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(HtmlControlDto model)
        {
            _contentService.SaveHtmlControl(model);
            return RedirectToAction("Index");
        }

        [EnetaAuthorize(Roles = "Administrator")]
        public ActionResult Details(long id)
        {
            return View(_contentService.GetHtmlControl(id));
        }

        [EnetaAuthorize(Roles = "Administrator")]
        public ActionResult Delete(long id)
        {
            _contentService.DeleteHtmlControl(id);
            return RedirectToAction("Index");
        }


        public PartialViewResult SidebarHtmlControls()
        {
            var model = _contentService.ListVisibleHtmlControls();
            return PartialView("_SidebarHtmlControls", model);
        }
    }
}
